﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebMantAsociados : System.Web.UI.Page
{

    String SQL, mssg;
    datos.Service aux = new datos.Service();
    System.Data.DataSet dst = new System.Data.DataSet();
    private void habilitarExiste()
    {
        Btnagregar.Enabled = false;
        Btnmodificar.Enabled = true;
        Btneliminar.Enabled = true;
    }
    private void habilitarNuevo()
    {
        Btnagregar.Enabled = true;
        Btnmodificar.Enabled = false;
        Btneliminar.Enabled = false;
    }
    private void deshabilitarBotones()
    {
        Btnagregar.Enabled = false;
        Btnmodificar.Enabled = false;
        Btneliminar.Enabled = false;
        ventana.LimpiartextBoxes(this);
    }
    formato ventana = new formato();
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            SQL = "Select *From tbAsociados Where AsociadoID = '" + TxtBid.Text + "'";
            aux.obtenerDatos(ref mssg, SQL, ref dst);
            if (dst.Tables[0].Rows.Count > 0)
            {
                TxtBnombre.Text = dst.Tables[0].Rows[0][2].ToString();
                TxtBapellido.Text = dst.Tables[0].Rows[0][1].ToString();
                TxtBdireccion.Text = dst.Tables[0].Rows[0][3].ToString();
                TxtBtelefono.Text = dst.Tables[0].Rows[0][4].ToString();
                habilitarExiste();
            }
            else
            {
                ventana.MessageBox("Registro no encontrado!", this);
                habilitarNuevo();
                Session["ID"] = TxtBid.Text;
                ventana.LimpiartextBoxes(this);
                TxtBid.Text = Session["ID"].ToString();
            }
        }
        catch (Exception ex)
        {
            ventana.MessageBox(ex.Message, this);
        }
    }

    protected void Btneliminar_Click(object sender, EventArgs e)
    {
        try
        {
            SQL = "Delete from tbAsociados Where AsociadoID= '" + TxtBid.Text + "'";
            aux.actualizarDatos(SQL, ref mssg);
            ventana.MessageBox("Registro eliminado", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            ventana.MessageBox(ex.Message, this);
        }

    }
    protected void Btnmodificar_Click(object sender, EventArgs e)
    {
        try
        {
          
            SQL = "Update tbAsociados set Apellidos='" + TxtBapellido.Text + "'";
            SQL += ",Nombre = '" + TxtBnombre.Text + "'";
            SQL += ",Direccion = '" + TxtBdireccion.Text + "'";
            SQL += ",Telefono= '" + TxtBtelefono.Text + "'";
            SQL += " Where AsociadoID= '" + TxtBid.Text + "'";
            Response.Write(SQL);
            aux.actualizarDatos(SQL, ref mssg);
            ventana.MessageBox("Registro modificado", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            ventana.MessageBox(ex.Message, this);
        }

    }
    protected void Btnagregar_Click(object sender, EventArgs e)
    {
        try
        {
           ;
            SQL = "Insert into tbAsociados(AsociadoID,Apellidos,Nombre,Direccion,Telefono)";
            SQL += " Values('" + TxtBid.Text + "','" + TxtBapellido.Text + "','" + TxtBnombre.Text + "','";
            SQL += TxtBdireccion.Text + "','" + TxtBtelefono.Text + "','";
            aux.actualizarDatos(SQL, ref mssg);
            ventana.MessageBox("Registro ingresado exitosamente!", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            ventana.MessageBox(ex.Message, this);
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TxtBnombre_TextChanged(object sender, EventArgs e)
    {

    }
}